//
//  SecondLevelMenuViewController.m
//  CenturyInterconnection
//
//  Created by 郭先生 on 16/4/6.
//  Copyright © 2016年 Shanghai DuRui Information Technology Company. All rights reserved.
//

#import "SecondLevelMenuViewController.h"
#import "LeftMenuModel.h"
#import "LeftMenuViewController.h"
#import "HomePageViewController.h"
#import "ProductListViewController.h"

@interface SecondLevelMenuViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView*tableView;
@property(nonatomic,strong)NSIndexPath*index;



@end

@implementation SecondLevelMenuViewController


-(UITableView *)tableView{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped ];
        _tableView.frame = CGRectMake(0, 64, KScreenWidth-ACTUAL_WIDTH(50), KScreenHeight-64);
        _tableView.delegate  =self;
        _tableView.dataSource=self;
    }
    return _tableView;
}

-(void)viewDidLoad{
    [super viewDidLoad];
    
    //[self getDatas];
    
    self.view.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.tableView];
    //_titleArray=[[NSMutableArray alloc]init];
    [self makeView];
    
    
    [[NSNotificationCenter defaultCenter] addObserverForName:SlideNavigationControllerDidClose object:nil queue:nil usingBlock:^(NSNotification *note) {
        NSString *menu = note.userInfo[@"menu"];
        NSLog(@"Closed %@", menu);
        [self dismissSecondLevel];
    }];
}

//-(void)getDatas{
//    
//    if (self.titleID==nil) {
//        self.titleID=@"0";
//    }
//    
//    // http://viprunningonline.com/api/index.php?main_page=category&cPath=5
//    NSString*urlStrr=[NSString stringWithFormat:@"%@",HTTP_ADDRESS];
//    NSDictionary*paramss=@{@"main_page":@"category",
//                           @"cPath":self.titleID};
//    HttpManager*managerr=[[HttpManager alloc]init];
//    [managerr getDataFromNetworkWithUrl:urlStrr parameters:paramss compliation:^(id data, NSError *error) {
//        
//        NSString*number=[NSString stringWithFormat:@"%@",data[@"status"]];
//        
//        if ([number isEqualToString:@"1"]) {
////            for (int i=0; i<[data[@"data"] count]; i++) {
////                //LeftMenuModel * model=[[LeftMenuModel alloc] initWithShopDict:data[@"data"][i]];
////                //[self.titleArray addObject:model];
////            }
//            
//            if ([data[@"data"] count]>0) {
//                [self.titleArray addObjectsFromArray:data[@"data"]];
//            }
//            
//            [self.tableView reloadData];
//        }
//    }];
//}


-(void)makeView{
    
    UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(20), ACTUAL_HEIGHT(30), KScreenWidth-ACTUAL_WIDTH(120), ACTUAL_HEIGHT(30))];
    titleLabel.text=self.titleStr.uppercaseString;
    titleLabel.font = FONT_SYSTEM_28;
    titleLabel.textColor = RGBCOLOR(204, 204, 204, 1);
    [self.view addSubview:titleLabel];
    
    UIImageView *backImageView=[[UIImageView alloc] initWithFrame:CGRectMake(ACTUAL_WIDTH(290), ACTUAL_HEIGHT(35), ACTUAL_WIDTH(9), ACTUAL_HEIGHT(14))];
    [backImageView setImage:[UIImage imageNamed:@"menu_back"]];
    [self.view addSubview:backImageView];
    
    UIButton *backButton=[[UIButton alloc] initWithFrame:CGRectMake(ACTUAL_WIDTH(0), ACTUAL_HEIGHT(20), KScreenWidth-ACTUAL_WIDTH(50), ACTUAL_HEIGHT(44))];
    [backButton addTarget:self action:@selector(dismissSecondLevel) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
}

-(void)dismissSecondLevel{
    
    //[self.view removeFromSuperview];
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

#pragma mark - -------- tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.titleArray.count <=0) {
        return 1;
    }
    return self.titleArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ACTUAL_HEIGHT(55);
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellID = @"mycell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    
    //LeftMenuModel *model= [[LeftMenuModel alloc] initWithShopDict:self.titleArray[indexPath.row]];
    cell.textLabel.text=[NSString stringWithFormat:@"              %@",self.titleArray[indexPath.row][@"categories_name"]];
    cell.textLabel.font = FONT_SYSTEM_24;
    cell.textLabel.textColor = RGBCOLOR(51, 51, 51, 1);
    return cell;
    
}

#pragma mark 点击返回数据给主控制器
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    id childdata = self.titleArray[indexPath.row][@"child"];
    
    if ([childdata isKindOfClass:[NSArray class]] && [childdata count] > 0) {
        
        self.titleStr = self.titleArray[indexPath.row][@"categories_name"];
        self.titleArray = self.titleArray[indexPath.row][@"child"];
        [self.tableView reloadData];
    }else{
        
        //LeftMenuModel *model= [[LeftMenuModel alloc] initWithShopDict:self.titleArray[indexPath.row]];
        
        ProductListViewController * proListVC = [[ProductListViewController alloc] init];
        proListVC.ListOfSpeciesStr = @"products_list";
        proListVC.cPathStr = [self.titleArray[indexPath.row] valueForKey:@"id"] ;
        proListVC.categories_name = [self.titleArray[indexPath.row] valueForKey:@"categories_name"] ;
        
        [self dismissSecondLevel];
        
        [[SlideNavigationController sharedInstance] pushViewController:proListVC animated:YES];
    }
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self dismissSecondLevel];
    
    [[NSNotificationCenter defaultCenter] removeObserver:SlideNavigationControllerDidClose];
}


@end
